#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
----------------------------------------
# @File     : makewc.py                 
# @Date     : 2021/4/4 22:38
# @Author   : 何启哲
# @Email    : heqizhehandsome@163.com
# @Software : PyCharm
----------------------------------------
"""
try:
    import wordcloud
    import PIL.Image as pil
    import numpy as np
    import matplotlib.pyplot as plt
    import jieba
except ImportError:
    import tkinter.messagebox
    tkinter.messagebox.showerror("ImportError", "请先下载相关库(wordcloud, pillow, numpy, matplotlib, jieba)!")

wcw = None              # word cloud width;  type: int
wch = None              # word cloud height; type: int
mask_file = None                                # type: str
save_picture = None                        # type: bool
bg_color = None                            # type: str
most_word = None                           # type: int
biggest_size = None                        # type: int

words = None

def makewc(wcw, wch, mask_file, save_picture, bg_color, most_word, biggest_size, words):
    words = jieba.lcut(words)
    words = " ".join(words)
    wc = wordcloud.WordCloud(
        font_path=r"C:\Windows\Fonts\simfang.ttf",
        width=wcw,
        height=wch,
        mask=np.array(pil.open(mask_file)) if mask_file != None else None,
        max_words=most_word,
        max_font_size=biggest_size,
        background_color = bg_color,
        mode='RGBA',
        repeat=True
    ).generate(words)
    plt.imshow(wc, interpolation='bilinear')
    plt.axis('off')
    plt.show()
    if save_picture:
        wc.to_file("wordcloud.png")